#include "StateMain.h"

#include "MyGame.h"
#include "Hollywood.h"

#define BG_X_MOVEMENT -3

StateMain::StateMain()
{
    nullify = false;

    bg.loadSprite("images/backgrounds/level_1_bg_test.png");
    bg.setPosition(-400,-160);
    bgYpos = -160.0;
    bgYmovement = 0;

    bgMovementCounter.init(500,MILLI_SECONDS,(void*) this, &StateMain::timerCallback);
    bgMovementCounter.setRewind(REWIND);
    bgMovementCounter.start();

    theme.loadMusic("music/wandor_title_theme.ogg");
    theme.play();

    title.loadFont("font/Freebooter.ttf", 144);
    title.setColour(Colour(BLACK));
    title.setPosition(60,60);

    mainMenu.setMenuStart(60,180);
    mainMenu.loadFont("font/Freebooter.ttf", 42);
    mainMenu.setTextColour(BLACK);
    mainMenu.setTextSelectionColour(RED);
    mainMenu.addItem(MENU_STRING_ITEM);
    mainMenu.setMenuItemText("New Game");
    mainMenu.addItem(MENU_STRING_ITEM);
    //mainMenu.setMenuItemText("Continue");
    //mainMenu.addItem(MENU_STRING_ITEM);
    mainMenu.setMenuItemText("Select Custom Level");
    mainMenu.addItem(MENU_STRING_ITEM);
    mainMenu.setMenuItemText("---");
    mainMenu.setIsSelectable(false);
    mainMenu.addItem(MENU_STRING_ITEM);
    mainMenu.setMenuItemText("Exit");

    fileBrowser.loadFont("font/unispace.ttf",12);
    fileBrowser.setTextColour(BLACK);
    fileBrowser.setTextSelectionColour(RED);
    fileBrowser.addFilter("txt");
    fileBrowser.addFilter("map");
    fileBrowser.addFilter("lst");
    fileBrowser.addFilter("enc");

    // default state
    state = stMenu;

    EFFECTS->fadeIn(1000,BLACK);
}


StateMain::~StateMain()
{
    theme.stop();
}


void StateMain::userInput()
{
    input->update();

#ifdef PLATFORM_PC
    if(input->isQuit())
    {
        nullifyState();
        return;
    }
#endif

    if (input->isStart())
        setNextState(STATE_TITLE);

    switch (state)
    {
    case stMenu:
        if (input->isUp())
            mainMenu.menuUp();
        else if (input->isDown())
            mainMenu.menuDown();

        if (input->isA()) // select menu item
        {
            switch (mainMenu.getSelection())
            {
            case 0: // New game
            //case 1: // Continue
                MyGame::GetMyGame()->loadLevelList("levels.lst");
                //MyGame::GetMyGame()->SetLevelFileName("level1-1.map");
                setNextState(STATE_LEVEL);
                break;
            case 1: // Custom level
                state = stLevelSelect;
                break;
            case 3: // Exit
                nullifyState();
                break;
            default:
                break;
            }
        }
        break;
    case stLevelSelect:
        if(input->isUp())
            fileBrowser.menuUp();
        else if(input->isDown())
            fileBrowser.menuDown();

        if(input->isA())
        {
            string choice = fileBrowser.enter();
            if (choice.find(".lst") != choice.npos)
            {
                MyGame::GetMyGame()->loadLevelList(choice);
                setNextState(STATE_LEVEL);
            }
            else
            {
                MyGame::GetMyGame()->SetLevelFileName(choice);
                setNextState(STATE_LEVEL);
            }
        }
        else if (input->isB())
            state = stMenu;
        break;
    }

    input->resetKeys();
}

void StateMain::update()
{
    EFFECTS->update();
    // BG movement
    bgMovementCounter.update();
    bg.setX(bg.getX() + BG_X_MOVEMENT);
    bgYpos += bgYmovement;
    bg.setY(bgYpos);
    if (bg.getX() <= -1600)
        bg.setX(0 + (bg.getX() + 1600));
    // states
    switch(state)
    {
    case stMenu:
        mainMenu.update();
        break;
    case stLevelSelect:
        fileBrowser.update();
        break;
    }
}

void StateMain::render()
{
    GFX::clearScreen();

    // render BG
    bg.render();
    if (bg.getX() < -800)
    {
        int prevPos = bg.getX();
        bg.setX(1600 + bg.getX());
        bg.render();
        bg.setX(prevPos);
    }

    // states
    switch(state)
    {
    case stMenu:
        title.print("Wandor");
        mainMenu.render();
        break;
    case stLevelSelect:
        fileBrowser.render();
        break;
    }
    EFFECTS->render();
}

void StateMain::timerCallback(void* object)
{
    StateMain* self = (StateMain*) object;
    self->bgYmovement = sin(self->bg.getX());
}
